package com.base.controller;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.sqlparam.FreeroutesExamplePage;
import com.base.po.voip.Freeroutes;
import com.base.po.voip.FreeroutesExample;
import com.base.po.voip.FreeroutesExample.Criteria;
import com.base.service.FreeroutesService;
import com.base.service.LogsService;
import com.base.utils.IP;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_feilvguanli {
	@Autowired
	private FreeroutesService freeroutesService;
	@Autowired
	private LogsService logsService;// log日志

	// 用户首页
	@RequestMapping("/content_feilvguanli")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		FreeroutesExamplePage examplePage = new FreeroutesExamplePage();
		List<Freeroutes> freeroutes = null;
		examplePage.pagesize = 10;
		examplePage.start = 0;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("CreateTime");
		examplePage.getOredCriteria().add(examplePage.createCriteria().andDatastatusEqualTo(true));
		freeroutes = freeroutesService.selectByExamplePage(examplePage);
		if (freeroutes != null) {
			/*
			 * 设置分页
			 */
			int totlenum = freeroutesService.countByExample(examplePage);// 页面总数
			request.setAttribute("totlenum", totlenum);// 传入session中
			request.setAttribute("curPage", 1);// 传入session中
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_feilvguanli");
			request.setAttribute("list", freeroutes);
			request.setAttribute("adminUsergrade", adminUsergrade);
			System.out.println(freeroutes.toString());
			return modelAndView;
		}
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		logsService.insert(CommContent.TYPE_SYS, CommContent.LOGS_READ + "费率", usernamesys, true,
				IP.getIpAddr(request));
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}

	// 用户首页
	@RequestMapping("/selectFreeroutes")
	public ModelAndView selectFreeroutes(HttpServletRequest request, String id, String name, BigDecimal feelroute,
			Integer datastatus, Integer curPage) throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		if (id == null) {
			if (request.getSession().getAttribute("id") != null) {
				id = (String) request.getSession().getAttribute("id");
			}
		}
		if (name == null) {
			if (request.getSession().getAttribute("name") != null) {
				name = (String) request.getSession().getAttribute("name");
			}
		}
		if (feelroute == null) {
			if (request.getSession().getAttribute("feelroute") != null) {
				feelroute = (BigDecimal) request.getSession().getAttribute("feelroute");
			}
		}
		if (datastatus == null) {
			if (request.getSession().getAttribute("datastatus") != null) {
				datastatus = (Integer) request.getSession().getAttribute("datastatus");
			} else {
				datastatus = 0;
			}
		}

		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		FreeroutesExamplePage examplePage = new FreeroutesExamplePage();
		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		List<Freeroutes> freeroutes = null;
		examplePage.pagesize = 10;
		examplePage.start = (curPage - 1) * examplePage.pagesize;
		examplePage.rows = examplePage.pagesize;// 设置首次分页
		examplePage.setOrderByClause("CreateTime");
		Criteria criteria = examplePage.createCriteria();
		examplePage.getOredCriteria().add(criteria.andDatastatusEqualTo(true));

		// 条件筛选
		if (id != null && !id.equals("")) {
			request.setAttribute("id", id);
			criteria = criteria.andIdEqualTo(id);
			examplePage.getOredCriteria().add(criteria);
		}
		if (name != null && !name.equals("")) {
			request.setAttribute("name", name);
			criteria = criteria.andNameLike("%" + name + "%");
			examplePage.getOredCriteria().add(criteria);
		}
		if (feelroute != null && feelroute != new BigDecimal(0)) {
			request.setAttribute("feelroute", feelroute);
			criteria = criteria.andFeelrouteLessThan(feelroute);
			examplePage.getOredCriteria().add(criteria);
		}
		// 判断当前可用性
		switch (datastatus) {
		case 2:
			examplePage.getOredCriteria().add(criteria.andDatastatusEqualTo(false));
			break;
		case 1:
			examplePage.getOredCriteria().add(criteria.andDatastatusEqualTo(true));
			break;
		case 0:
			break;
		}
		request.setAttribute("datastatus", datastatus);
		freeroutes = freeroutesService.selectByExamplePage(examplePage);
		if (freeroutes != null) {
			/*
			 * 设置分页
			 */
			int totlenum = freeroutesService.countByExample(examplePage);// 页面总数
			request.setAttribute("totlenum", totlenum);// 总数据传入session中
			request.setAttribute("curPage", curPage);// 当前页面传入session中
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_feilvguanli");
			request.setAttribute("list", freeroutes);
			request.setAttribute("adminUsergrade", adminUsergrade);
			System.out.println(freeroutes.toString());
			return modelAndView;
		}
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}

	String SUCCESS = "success";

	// 修改费率
	@RequestMapping(value = "/updateFreeroutes", method = RequestMethod.POST)
	public void updateFreeroutes(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id, String name, BigDecimal feelroute,
			Boolean datastatus, Integer ratelength) throws Exception {
		String usernamesys = (String) request.getSession().getAttribute("useracc");
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 验证重要信息
		if (id == null) {
			out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
			return;
		} else if (name != null) {
			FreeroutesExample freeroutesExample = new FreeroutesExample();
			freeroutesExample.getOredCriteria().add(freeroutesExample.createCriteria().andNameEqualTo(name));
			if (freeroutesService.selectByExample(freeroutesExample).size() > 0) {
				logsService.insert(CommContent.TYPE_UPDATE, "尝试修改费率，当时失败了", usernamesys, true, IP.getIpAddr(request));
				out.print("更新失败，请尝试更换费率名称");
				return;
			}
		} else {
			Freeroutes freeroutes = new Freeroutes(id, name, feelroute, datastatus, ratelength, null);
			int code = freeroutesService.updateByPrimaryKeySelective(freeroutes);
			if (code == 1) {
				logsService.insert(CommContent.TYPE_UPDATE, "修改费率：" + name, usernamesys, true, IP.getIpAddr(request));
				out.print(SUCCESS);
			} else {
				logsService.insert(CommContent.TYPE_UPDATE, "尝试修改费率，当时失败了", usernamesys, true, IP.getIpAddr(request));
				out.print("更改失败，请您重试");
			}
		}
	}

}
